﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using ElectronicCommerce.Framework.Utility;
using ElectronicCommerce.Framework.Web.Core;

namespace ElectronicCommerce.Framework.Web.Mvc.WebForm
{
    public class CustomControllerBase
    {
        private bool _builded = false;
        private CustomModelDictionary _modelDic = new CustomModelDictionary();

        #region Properties
        /// <summary>
        /// 当前页面上下文对象，包括 Http Request / Response 等
        /// </summary>
        public CustomContextBase CustomContext { get; set; }
        public Page ViewPage { get; set; }

        /// <summary>
        /// 当前页面所需对象
        /// </summary>
        public CustomModelDictionary Models
        {
            get
            {
                if (!_builded)
                {
                    InternalExecute();

                    _modelDic = BuildModels(_modelDic);

                    _builded = true;
                }
                return _modelDic;
            }
        }

        /// <summary>
        /// 设置页面 Master Page （Master Page 使用枚举列表）
        /// </summary>
        protected virtual Enum ViewMasterPageAlias { get { return null; } }

        /// <summary>
        /// 获取 Master Page File 路径
        /// </summary>
        public string MasterPageFile { get { return GetMasterPage(ViewMasterPageAlias); } }
        #endregion

        #region Methods
        #region Private
        private void InternalExecute()
        {
            if (CustomContext.HttpMethod == HttpMethodType.Post)
            {
                Post();
            }
            else
            {
                Get();
            }

            Execute();
        }
        #endregion

        /// <summary>
        /// 需要在页面 Controller 中重载，构建当前页面需要的数据对象 （models）
        /// </summary>
        /// <param name="modelDic"></param>
        /// <returns></returns>
        protected virtual CustomModelDictionary BuildModels(CustomModelDictionary modelDic)
        {
            return modelDic;
        }

        /// <summary>
        /// 当 GET 方式访问时执行
        /// </summary>
        protected virtual void Get()
        {

        }

        /// <summary>
        /// 当 POST 方式访问时执行
        /// </summary>
        protected virtual void Post()
        {

        }

        /// <summary>
        /// 执行页面操作，在 GET 或 POST 事件执行之后
        /// </summary>
        protected virtual void Execute()
        {

        }

        /// <summary>
        /// 转换 Master Page 实际文件路径，可以重载
        /// 默认文件路径："~/Views/Shared/"
        /// </summary>
        /// <param name="masterPageAlias"></param>
        /// <returns></returns>
        protected virtual string GetMasterPage(Enum masterPageAlias)
        {
            if (masterPageAlias != null)
            {
                return string.Format("~/Views/Shared/{0}.Master", masterPageAlias.ToString());
            }
            return string.Empty;
        }
        #endregion

    }

    public class CustomControllerBase<TCustomContext, TPageAliasType> : CustomControllerBase
        where TCustomContext : CustomContextBase
        where TPageAliasType : struct
    {
        new public TCustomContext CustomContext
        {
            get { return base.CustomContext as TCustomContext; }
            set { base.CustomContext = value; }
        }

        new public CustomViewPageBase<TCustomContext> ViewPage
        {
            get { return base.ViewPage as CustomViewPageBase<TCustomContext>; }
            set { base.ViewPage = value; }
        }

        new public CustomAjaxPageBase<TCustomContext> AjaxPage
        {
            get { return base.ViewPage as CustomAjaxPageBase<TCustomContext>; }
            set { base.ViewPage = value; }
        }

        public TPageAliasType ViewPageAlias { get { return Converter.Enum<TPageAliasType>(ViewPage.InternalViewPageAlias); } }
    }
}
